﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


public class CharacterModel : SingletonTemplate<CharacterModel>
{
    private static class KeyNames
    {
        public static string LEVEL = "level";
        public const string EX = "Ex";
        public const string CALC_TIME = "CalcTime";
    }
    private int m_level;
    public int Level { get { return m_level; } }

    public int Attack { get { return Level * 30; } }
    public int Hp { get { return Level * 50; } }
    public int Mp { get { return Level * 50; } }
    public int Defense { get { return Level * 30; } }
    public int Ex { get { return PlayerPrefs.GetInt(KeyNames.EX); } }

    private int m_Ex;
    private DateTime m_lastCalcTime;
    public List<string> m_RecordList = new List<string>();

    public void Init()
    {
        m_level = PlayerPrefs.GetInt(KeyNames.LEVEL);
        var dtStr = PlayerPrefs.GetString(KeyNames.CALC_TIME, "");
        if (!string.IsNullOrEmpty(dtStr))
        {
            m_lastCalcTime = DateTime.Parse(dtStr);
            m_Ex = Ex;
        }
        else
        {
            m_lastCalcTime = DateTime.MinValue;
            m_Ex = 0;
        }
        CalcEx();
    }

    public bool CalcEx()
    {
        var now = DateTime.UtcNow;
        if (now > m_lastCalcTime && m_lastCalcTime != DateTime.MinValue)
        {
            var delta = (now - m_lastCalcTime).TotalSeconds;
            if (delta < GameGlobalConfig.Instance.addExSceond || GameGlobalConfig.Instance.addExSceond == 0)
            {
                return false;
            }
            m_lastCalcTime = now;
            var config = CharacterLevelConfig.Instance.GetItem(m_level);
            if (config == null)
            {
                Debug.Log("[CharacterModel][CalcEx] level dont have config level:" + m_level);
                return false;
            }
            var ex = Mathf.Floor((float)delta / GameGlobalConfig.Instance.addExSceond) * config.exAdd * 10;
            var preEx = m_Ex;
            m_Ex += (int)ex;
            AddInfo("获得经验:" + ex);
            while (m_Ex >= config.levelUp)
            {
                config = CharacterLevelConfig.Instance.GetItem(CharacterModel.Instance.Level + 1);
                if (config == null)
                {
                    Debug.Log("[CharacterModel][CalcEx] level dont have config level:" + m_level);
                    return false;
                }
                m_Ex -= config.levelUp - preEx;
                preEx = 0;

                PlayerPrefs.SetInt(KeyNames.LEVEL, m_level);
            }
            PlayerPrefs.SetInt(KeyNames.EX, m_Ex);
            PlayerPrefs.SetString(KeyNames.CALC_TIME, now.ToString());
        }
        else
        {
            m_Ex = 0;
            m_level = 1;
            m_lastCalcTime = now;
            PlayerPrefs.SetInt(KeyNames.LEVEL, m_level);
            PlayerPrefs.SetInt(KeyNames.EX, m_Ex);
            PlayerPrefs.SetString(KeyNames.CALC_TIME, now.ToString());
        }
        return true;
    }

    public void AddInfo(string str)
    {
        m_RecordList.Add(str);
    }
}
